package cn.com.lms.business.exam.persistence.repository;

import cn.com.lms.business.exam.persistence.entity.Question;


import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;


@Repository
public interface QuestionRepository extends BaseRepository<Question, Long> {

    @Query(value = "select eq.id,eq.question_name as questionName,eq.is_private as isPrivate,  (" +
            "select count(0) from bnz_exam_question_subject qs where qs.question_id=eq.id and qs.is_delete=0 ) count " +
            "from bnz_exam_question  eq where eq.is_delete=0 ", nativeQuery = true)
    List<Map> queryQuestion();

    @Query(value = "select eq.id,eq.question_name as questionName,eq.category_id as categoryId,eq.is_private as isPrivate,eq.is_delete as isDelete,eq.creator,eq.create_time as createTIme,eq.modify_by as modifyBy,eq.modify_time  modifyTime,eq.creator_name as creatorName,eq.modify_name as modifyName,  (" +
            "select count(0) from bnz_exam_question_subject qs where qs.question_id=eq.id and qs.is_delete=0 ) count " +
            "from bnz_exam_question  eq where eq.is_delete=0 and eq.id =:id", nativeQuery = true)
    Map findQuestionById(@Param("id") Long id);

    @Transactional
    @Modifying
    @Query(value = "update bnz_exam_question set is_delete =:isDelete WHERE id in (:collection)", nativeQuery = true)
    int updateIsDelete(@Param("isDelete") Boolean isDelete , @Param("collection") Collection<Long> collection) ;

}